package gson.gson.Interface;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;

/*打印
User{userName='Alice', age=30, password='123'}
data with exclude password: {"userName":"Alice","age":30}
data with exclude user class: null
*/

public class CustomExclusionStrategy implements ExclusionStrategy {
    private final Class<?> excludedThisClass;

    public CustomExclusionStrategy(Class<?> excludedThisClass) {
        this.excludedThisClass = excludedThisClass;
    }

    // 排除指定类的所有字段
    @Override
    public boolean shouldSkipClass(Class<?> clazz) {
        return excludedThisClass != null && excludedThisClass.equals(clazz);
    }

    // 如果字段名为 "password"，则排除
    @Override
    public boolean shouldSkipField(FieldAttributes f) {
        return "password".equals(f.getName());
    }
}
